﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLNhaThuoc.DTO;
using QLNhaThuoc.MyClass;

namespace QLNhaThuoc.DAL
{
    internal class MappingNhanVienDA : MappingDA
    {
        private const string SuccessString = "Mapping thành công";
        private const string FailureString = "Cần mapping đầy đủ các field Mã nhân viên, Loại nhân viên";
        public override string mappingXMLToDB(List<ExpressionDTO> dataExpression)
        {
            List<NhanVien> lstNhanVien = new List<NhanVien>();
            NhanVienDA nhanVienDA = new NhanVienDA();

            for (int i = 0; i < mapping.Data.Count; i++)
            {
                NhanVien nhanVien;
                int iTemp = -1;
                
                iTemp = exists("LoaiNV", dataExpression);
                if (iTemp != -1)
                {
                    string loaiNV = returnEvaluated(i, dataExpression[iTemp]);
                    NhanVienFactory nvFactory = new NhanVienFactory();
                    nhanVien = nvFactory.TaoNhanVien(loaiNV);
                }
                else
                {
                    return "Loại nhân viên không phù hợp";
                }

                iTemp = exists("MaNV", dataExpression);
                if (iTemp != -1)
                {
                    nhanVien.MaNV = returnEvaluated(i, dataExpression[iTemp]);
                }
                else
                {
                    return FailureString;
                }

                iTemp = exists("Ho", dataExpression);
                nhanVien.Ho = iTemp != -1 ? returnEvaluated(i, dataExpression[iTemp]) : string.Empty;

                iTemp = exists("GioiTinh", dataExpression);
                nhanVien.GioiTinh = iTemp == -1 || bool.Parse(returnEvaluated(i, dataExpression[iTemp]));

                iTemp = exists("NgaySinh", dataExpression);
                nhanVien.NgaySinh = iTemp != -1 ? DateTime.Parse(returnEvaluated(i, dataExpression[iTemp])) : DateTime.Now;

                iTemp = exists("DiaChi", dataExpression);
                nhanVien.DiaChi = iTemp != -1 ? returnEvaluated(i, dataExpression[iTemp]) : string.Empty;

                iTemp = exists("SDT", dataExpression);
                nhanVien.SDT = iTemp != -1 ? returnEvaluated(i, dataExpression[iTemp]) : string.Empty;

                iTemp = exists("NgayVaoLam", dataExpression);
                nhanVien.NgayVaoLam = iTemp != -1 ? DateTime.Parse(returnEvaluated(i, dataExpression[iTemp])) : DateTime.Now;

                iTemp = exists("Username", dataExpression);
                nhanVien.Username = iTemp != -1 ? returnEvaluated(i, dataExpression[iTemp]) : string.Empty;

                iTemp = exists("Password", dataExpression);
                nhanVien.Password = iTemp != -1 ? returnEvaluated(i, dataExpression[iTemp]) : string.Empty;

                iTemp = exists("Ten", dataExpression);
                nhanVien.Ten = iTemp != -1 ? returnEvaluated(i, dataExpression[iTemp]) : string.Empty;

                lstNhanVien.Add(nhanVien);
            }

            foreach (NhanVien info in lstNhanVien)
            {
                nhanVienDA.insert(info);
            }

            return SuccessString;
        }
    }
}
